package jp.co.cos.ittrick.channel.controller.admin;

import jp.co.cos.ittrick.channel.model.ApplicationModel;
import jp.co.cos.ittrick.channel.service.ApplicationService;
import jp.co.cos.ittrick.channel.service.FileService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.datastore.Text;

/**
 * Application コントロール
 * @author takahara
 *
 */
public class ApplicationController extends Controller {

    private ApplicationService applicationService;
    private FileService service;

    public ApplicationController() {
        applicationService = new ApplicationService();
        service = new FileService();
    }

    @Override
    public Navigation run() throws Exception {

        ApplicationModel applicationModel = applicationService.getApplicationModel();

        if(applicationModel == null) {
            return forward("application.jsp");
        }

        // カスタムタグのエンエスケープ
        String content = applicationModel.getContentString();
        applicationModel.setApplContent(new Text(content));

        requestScope("applicationModel", applicationModel);
        requestScope("dataList", service.getDataListOfApplication());

        if(applicationModel != null) {

            // キーワード
            String keyWord = null;
            for(String keyWordTmp: applicationModel.getKeyWordList()) {
                if(keyWord == null) {
                    keyWord = keyWordTmp;
                }else {
                    keyWord = keyWord + "," + keyWordTmp;
                }
            }
            requestScope("keyWord", keyWord);
        }

        return forward("application.jsp");
    }
}
